{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Most Frequent Even Element"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: mostFrequentEven"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #出现最频繁的偶数元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，返回出现最频繁的偶数元素。</p>\n",
    "\n",
    "<p>如果存在多个满足条件的元素，只需要返回 <strong>最小</strong> 的一个。如果不存在这样的元素，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [0,1,2,2,4,4,1]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>\n",
    "数组中的偶数元素为 0、2 和 4 ，在这些元素中，2 和 4 出现次数最多。\n",
    "返回最小的那个，即返回 2 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [4,4,4,9,2,4]\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>4 是出现最频繁的偶数元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [29,47,21,41,13,37,25,7]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>不存在偶数元素。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 2000</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [most-frequent-even-element](https://leetcode.cn/problems/most-frequent-even-element/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [most-frequent-even-element](https://leetcode.cn/problems/most-frequent-even-element/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,1,2,2,4,4,1]', '[4,4,4,9,2,4]', '[29,47,21,41,13,37,25,7]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        hashmap = {}\n",
    "        length = len(nums)\n",
    "        count = 0\n",
    "        result = -1\n",
    "        for i in range(length):\n",
    "            if nums[i] % 2 == 0:\n",
    "                hashmap[nums[i]] = hashmap.get(nums[i] , 0) + 1\n",
    "        for key in hashmap.keys():\n",
    "            if hashmap[key] > count:\n",
    "                result = key\n",
    "                count = hashmap[key]\n",
    "            elif hashmap[key] == count and key < result:\n",
    "                result = key\n",
    "                count = hashmap[key]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        n = len(nums)\n",
    "        cnt = [0] * 100005\n",
    "        res = -1\n",
    "        m = -1\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                cnt[x] += 1\n",
    "                if cnt[x] > m:\n",
    "                    m = cnt[x]\n",
    "                    res = x\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnts = [0]*100001\n",
    "        for num in nums:\n",
    "            if num%2==0:\n",
    "                cnts[num] += 1\n",
    "        ans = -1\n",
    "        freq = 0\n",
    "        for i in range(0, 100001, 2):\n",
    "            if cnts[i]>freq:\n",
    "                ans = i\n",
    "                freq = cnts[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        s = defaultdict(int)\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                s[i] += 1\n",
    "        ans = -1\n",
    "        max_times = 0\n",
    "        for k, v in s.items():\n",
    "            if v > max_times:\n",
    "                ans = k\n",
    "                max_times = v\n",
    "            elif v == max_times:\n",
    "                ans = min(ans, k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(nums)\n",
    "        print(cnt)\n",
    "        d = sorted(cnt.items(), key = lambda x: x[1], reverse=True)\n",
    "        maxx = inf\n",
    "        flag = False\n",
    "        res = inf\n",
    "        for i, v in d:\n",
    "            if not i & 1:\n",
    "                if not flag:\n",
    "                    maxx = v\n",
    "                    res = i\n",
    "                    flag = True\n",
    "                else:\n",
    "                    if v == maxx:\n",
    "                        res = min(i, res)                    \n",
    "        return -1 if res == inf else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        m = 1\n",
    "        for n in nums:\n",
    "            if n % 2 == 0:\n",
    "                if n not in dic:\n",
    "                    dic[n] = 1\n",
    "                else:\n",
    "                    dic[n] += 1\n",
    "                    if dic[n] > m:\n",
    "                        m = dic[n]\n",
    "        if dic:\n",
    "            ans = 100000\n",
    "        else:\n",
    "            ans = -1\n",
    "        for k, v in dic.items():\n",
    "            if v == m and k < ans:\n",
    "                ans = k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        new = []\n",
    "        for i in nums:\n",
    "            if i%2 == 0:\n",
    "                new.append(i)\n",
    "        if new == []:\n",
    "            return -1\n",
    "        t = [0 for _ in range(100002)]\n",
    "        max = 0\n",
    "        ind = -1\n",
    "        for x in new:\n",
    "            t[x] += 1\n",
    "            if t[x] > max:\n",
    "                max = t[x]\n",
    "                ind = x\n",
    "            elif t[x] == max:\n",
    "                if x < ind:\n",
    "                    ind = x\n",
    "        return ind\n",
    "        # temp = collections.Counter(new)\n",
    "        # tt = sorted(temp.items(), key=lambda x:x[1],reverse=True)\n",
    "        # return tt[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = Counter()\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                cnt[num] += 1\n",
    "            if cnt[num] > cnt[ans] or cnt[num] == cnt[ans] and num < ans:\n",
    "                ans = num\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        count = Counter()\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                count[x] += 1\n",
    "        res, ct = -1, 0\n",
    "        for k, v in count.items():\n",
    "            if res == -1 or v > ct or (v == ct and res > k):\n",
    "                res = k\n",
    "                ct = v\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(x for x in nums if x % 2 == 0)\n",
    "        if len(cnt) == 0:\n",
    "            return -1\n",
    "        max_cnt = max(cnt.values())\n",
    "        return min(x for x,c in cnt.items() if c == max_cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        pre , ans = 0 , 0x3f3f3f3f\n",
    "        for num in nums :\n",
    "            if num % 2 != 0 : continue \n",
    "            if nums.count(num) > pre  or nums.count(num) == pre and ans > num :\n",
    "                ans = num\n",
    "                pre = nums.count(num)\n",
    "        return ans if ans != 0x3f3f3f3f else  -1 \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        m = 1\n",
    "        for n in nums:\n",
    "            if n % 2 == 0:\n",
    "                if n not in dic:\n",
    "                    dic[n] = 1\n",
    "                else:\n",
    "                    dic[n] += 1\n",
    "                if dic[n] > m:\n",
    "                    m = dic[n]\n",
    "        return min([k for k, v in dic.items() if v == m]) if dic else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt=Counter(num for num in nums if num%2==0)\n",
    "        if len(cnt)==0: return -1\n",
    "        max_cnt=max(cnt.values())\n",
    "        return min(x for x,c in cnt.items() if c==max_cnt)\n",
    "        return 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        count = 0\n",
    "        count_max = 0\n",
    "        key = None\n",
    "        for date in nums:\n",
    "            if date % 2 == 0:\n",
    "                count = nums.count(date)\n",
    "                if count >= count_max:\n",
    "                    count_max = count\n",
    "                    key = date\n",
    "        if key == None:\n",
    "            return -1\n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        odd_dict = {}\n",
    "        max_num_flag = {\"num\": -1, \"time\": 0}\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i not in odd_dict.keys():\n",
    "                    odd_dict[i] = 1\n",
    "                else:\n",
    "                    odd_dict[i] += 1\n",
    "                if (odd_dict[i] > max_num_flag[\"time\"]) or (\n",
    "                        odd_dict[i] == max_num_flag[\"time\"] and i < max_num_flag[\"num\"]):\n",
    "                    max_num_flag['num'] = i\n",
    "                    max_num_flag['time'] = odd_dict[i]\n",
    "        return max_num_flag['num']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums) :\n",
    "        d={}\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]%2==0:\n",
    "                d[nums[i]]=d.get(nums[i],0)+1\n",
    "        b=[]\n",
    "        for i in d.keys():\n",
    "            if max(d.values())==d[i]:\n",
    "                b.append(i)\n",
    "        return min(b) if b else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        result_num = 0\n",
    "        result = -1\n",
    "        dic_num = Counter(nums)\n",
    "        for key,val in dic_num.items():\n",
    "            if key%2 == 0:\n",
    "                if val > result_num:\n",
    "                    result = key\n",
    "                    result_num = val\n",
    "                elif val==result_num:\n",
    "                    if key < result:\n",
    "                        result = key\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        count = Counter()\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                count[x] += 1\n",
    "        res, ct = -1, 0\n",
    "        for k, v in count.items():\n",
    "            if res == -1 or v > ct or (v == ct and res > k):\n",
    "                res = k\n",
    "                ct = v\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        counter = Counter(num for num in nums if num % 2 == 0)\n",
    "        if not counter:\n",
    "            return -1\n",
    "        freq = max(counter.values())\n",
    "        return min(num for num, cnt in counter.items() if cnt == freq)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        m = 1\n",
    "        for n in nums:\n",
    "            if n % 2 == 0:\n",
    "                if n not in dic:\n",
    "                    dic[n] = 1\n",
    "                else:\n",
    "                    dic[n] += 1\n",
    "                if dic[n] > m:\n",
    "                    m = dic[n]\n",
    "        return min([k for k, v in dic.items() if v == m]) if dic else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        jishu=0\n",
    "        dic1 = {}\n",
    "        dic2 = {}\n",
    "        for i in nums:\n",
    "            if (i%2)==0:\n",
    "                jishu = (nums.count(i))\n",
    "                dic1 = dict.fromkeys((str(i),),jishu)\n",
    "                dic2.update(dic1)\n",
    "        result=[]\n",
    "        for key in dic2:\n",
    "            if dic2[key] == max(dic2.values()):\n",
    "                result.append(int(key))\n",
    "        if not result:\n",
    "            return -1\n",
    "        else:\n",
    "            return min(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = Counter()\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                cnt[num] += 1\n",
    "            if cnt[num] > cnt[ans] or cnt[num] == cnt[ans] and num < ans: # and优先级高于or\n",
    "                ans = num\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        keynum = -1\n",
    "        biggest = 0\n",
    "        aSet = set()\n",
    "        adic = {}\n",
    "        for num in nums:\n",
    "            if num%2 == 0: \n",
    "                if not num in adic.keys():\n",
    "                  aSet.add(num)\n",
    "                  adic[num]=1\n",
    "                else:\n",
    "                  adic[num] = adic[num]+1\n",
    "                if biggest < adic[num]:\n",
    "                    biggest = adic[num]\n",
    "                    keynum = num\n",
    "                elif biggest == adic[num]:\n",
    "                    if keynum > num:\n",
    "                       keynum = num \n",
    "        return keynum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        count = 0\n",
    "        count_max = 0\n",
    "        key = None\n",
    "        for date in nums:\n",
    "            if date % 2 == 0:\n",
    "                count = nums.count(date)\n",
    "                if count >= count_max:\n",
    "                    count_max = count\n",
    "                    key = date\n",
    "        if key == None:\n",
    "            return -1\n",
    "        return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        lst = [n for n in nums if n % 2 == 0]\n",
    "        if lst:\n",
    "            ans = 100000\n",
    "        else:\n",
    "            return -1\n",
    "        dic = Counter(lst)\n",
    "        m = max(dic.values())\n",
    "        for k, v in dic.items():\n",
    "            if v == m and k < ans:\n",
    "                ans = k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        m = 1\n",
    "        for n in nums:\n",
    "            if n % 2 == 0:\n",
    "                if n not in dic:\n",
    "                    dic[n] = 1\n",
    "                else:\n",
    "                    dic[n] += 1\n",
    "                    if dic[n] > m:\n",
    "                        m = dic[n]\n",
    "        return min([k for k, v in dic.items() if v == m]) if dic else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        odd_dict = {}\n",
    "        max_num_flag = {\"num\": -1, \"time\": 0}\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i not in odd_dict.keys():\n",
    "                    odd_dict[i] = 1\n",
    "                else:\n",
    "                    odd_dict[i] += 1\n",
    "                if (odd_dict[i] > max_num_flag[\"time\"]) or (\n",
    "                        odd_dict[i] == max_num_flag[\"time\"] and i < max_num_flag[\"num\"]):\n",
    "                    max_num_flag['num'] = i\n",
    "                    max_num_flag['time'] = odd_dict[i]\n",
    "        return max_num_flag['num']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class counter():\n",
    "    def __init__(self,init=0):\n",
    "        self.dic={}\n",
    "        self.init=init\n",
    "    def __getitem__(self,key):\n",
    "        if self.dic.setdefault(key,None)==None:\n",
    "            self.dic[key]=self.init\n",
    "            return self.init\n",
    "        return self.dic[key]\n",
    "    \n",
    "    def __setitem__(self,key,val):\n",
    "        self.dic[key]=val\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = counter()\n",
    "        for x in nums:\n",
    "            if x % 2: continue  # 跳过奇数\n",
    "            cnt[x] += 1\n",
    "            if cnt[x] > cnt[ans] or cnt[x] == cnt[ans] and x < ans:\n",
    "                ans = x  # 出现次数最大的数中，值最小的\n",
    "        return ans\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums=list(filter(lambda x:x%2==0,nums))\n",
    "        c=Counter(nums)\n",
    "        if not c:\n",
    "            return -1\n",
    "        m=max(c.values())\n",
    "        for i in sorted(c.keys()):\n",
    "            if c[i]==m:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "    \n",
    "#     def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "#         jishu=0\n",
    "#         dic1 = {}\n",
    "#         dic2 = {}\n",
    "#         for i in nums:\n",
    "#             if (i%2)==0:\n",
    "#                 jishu = (nums.count(i))\n",
    "#                 dic1 = dict.fromkeys((str(i),),jishu)\n",
    "#                 dic2.update(dic1)\n",
    "#         result=[]\n",
    "#         for key in dic2:\n",
    "#             if dic2[key] == max(dic2.values()):\n",
    "#                 result.append(int(key))\n",
    "#         if not result:\n",
    "#             return -1\n",
    "#         else:\n",
    "#             return min(result)\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        count = Counter()\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                count[x] += 1\n",
    "        res, ct = -1, 0\n",
    "        for k, v in count.items():\n",
    "            if res == -1 or v > ct or (v == ct and res > k):\n",
    "                res = k\n",
    "                ct = v\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        dic={}\n",
    "        # 1、遍历数组，把偶数的出现次数统计到字典里。\n",
    "        for num in nums:\n",
    "            if num%2==0:\n",
    "                dic[num]=dic.get(num,0)+1\n",
    "        # print(dic)\n",
    "        if not dic:\n",
    "            return -1\n",
    "        dd=sorted(dic.items(),key=lambda x:x[1],reverse=True)\n",
    "        # 2、找偶数出现次数最多的值，就是最大出现次数\n",
    "        ans=max([x for x in dic.values()])\n",
    "        # print(ans)\n",
    "        # 3、把出现最高频的偶数取出来，放到列表里\n",
    "        res=[key for key,value in dic.items() if value==ans]\n",
    "        # print(res)\n",
    "        # 如果列表不为空，取最小值。\n",
    "        if res:\n",
    "            return min(res)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        a=0\n",
    "        b=100001\n",
    "        nums1=[]\n",
    "        for i in nums:\n",
    "            if i%2==0:\n",
    "                if i not in nums1:\n",
    "                    nums1.append((i,nums.count(i)))\n",
    "                    a=max(a,nums.count(i))\n",
    "        if nums1==[]:\n",
    "            return -1\n",
    "        else:\n",
    "            for i in nums1:\n",
    "                if i[1]==a:\n",
    "                    b=min(b,i[0])\n",
    "            return b        \n",
    "\n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        count = Counter()\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                count[x] += 1\n",
    "        res, ct = -1, 0\n",
    "        for k, v in count.items():\n",
    "            if res == -1 or v > ct or (v == ct and res > k):\n",
    "                res = k\n",
    "                ct = v\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = []\n",
    "        ns = []\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                ans.append(nums.count(i))\n",
    "        if ans:\n",
    "            for i in nums:\n",
    "                if i % 2 == 0:\n",
    "                    if nums.count(i) == max(ans):\n",
    "                        ns.append(i)\n",
    "            return min(ns)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        count = Counter()\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                count[x] += 1\n",
    "        res, ct = -1, 0\n",
    "        for k, v in count.items():\n",
    "            if res == -1 or v > ct or (v == ct and res > k):\n",
    "                res = k\n",
    "                ct = v\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        tmp=dict()\n",
    "        for i in nums:\n",
    "            if i in tmp:\n",
    "                tmp[i]+=1\n",
    "            else:\n",
    "                if i%2==0:\n",
    "                    tmp[i]=1\n",
    "        if not tmp:\n",
    "            return -1\n",
    "        tmp=sorted(tmp.items(),key=lambda x: (-x[1],x[0]))\n",
    "        return tmp[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(x for x in nums if x % 2 == 0)\n",
    "        ans, mx = -1, 0\n",
    "        for x, v in cnt.items():\n",
    "            if v > mx or (v == mx and ans > x):\n",
    "                ans, mx = x, v\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ad={}\n",
    "        for i in nums:\n",
    "            if i%2==0:\n",
    "                ad[i]=ad.get(i,0)+1\n",
    "        v=[v for v in ad.values()]\n",
    "        k=[k for k in ad.keys() if ad[k]==max(v)]\n",
    "        if k:\n",
    "            return min(k)\n",
    "        else:\n",
    "            return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        odd_dict = {}\n",
    "        max_num_flag = {\"num\": -1, \"time\": 0}\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i not in odd_dict.keys():\n",
    "                    odd_dict[i] = 1\n",
    "                else:\n",
    "                    odd_dict[i] += 1\n",
    "                if (odd_dict[i] > max_num_flag[\"time\"]) or (\n",
    "                        odd_dict[i] == max_num_flag[\"time\"] and i < max_num_flag[\"num\"]):\n",
    "                    max_num_flag['num'] = i\n",
    "                    max_num_flag['time'] = odd_dict[i]\n",
    "        return max_num_flag['num']\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = Counter()\n",
    "        for x in nums:\n",
    "            if x % 2: continue  # 跳过奇数\n",
    "            cnt[x] += 1\n",
    "            if cnt[x] > cnt[ans] or cnt[x] == cnt[ans] and x < ans:\n",
    "                ans = x  # 出现次数最大的数中，值最小的\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        count = Counter()\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                count[num] += 1\n",
    "        res = -1\n",
    "        sum = 0\n",
    "        for i, j in count.items():\n",
    "            if j > sum or (j == sum and i < res):\n",
    "                res = i\n",
    "                sum = j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                dic[num] = dic.get(num, 0) + 1\n",
    "        if not dic:\n",
    "            return -1\n",
    "        max_num = max(dic.values())\n",
    "        min_num = inf\n",
    "        for key, value in dic.items():\n",
    "            if value == max_num and key < min_num:\n",
    "                min_num = key\n",
    "        return min_num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        c = Counter(nums)\n",
    "        res, ans = -1, 0\n",
    "        for i in c:\n",
    "            if i % 2 == 0:\n",
    "                 if c[i] > ans:\n",
    "                     res = i\n",
    "                     ans = c[i]\n",
    "                 elif c[i] == ans and i < res:\n",
    "                     res = i\n",
    "                     \n",
    "                     \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt = {}\n",
    "        maxcnt, ans = 0, -1\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i not in cnt.keys():\n",
    "                    cnt[i] = 1\n",
    "                else:\n",
    "                    cnt[i] += 1\n",
    "        for k, v in cnt.items():\n",
    "            if v > maxcnt:\n",
    "                maxcnt = v\n",
    "                ans = k\n",
    "            elif v == maxcnt:\n",
    "                ans = min(ans, k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums = [i for i in nums if i % 2==0]\n",
    "        ans = []\n",
    "        ns = []\n",
    "        for i in nums:\n",
    "            ans.append(nums.count(i))\n",
    "        if ans:\n",
    "            for i in nums:\n",
    "                if nums.count(i) == max(ans):\n",
    "                    ns.append(i)\n",
    "            return min(ns)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        num_count = {}\n",
    "        nums = sorted(nums, reverse=True)\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                if num in num_count.keys():\n",
    "                    num_count[num] += 1\n",
    "                else:\n",
    "                    num_count[num] = 1\n",
    "        if len(num_count) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            sort_num_count = sorted(num_count.items(), key=lambda x:x[1])\n",
    "            return sort_num_count[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        tmp = 0\n",
    "        res = -1\n",
    "        for c in dic:\n",
    "            if c%2 == 0 and(dic[c] > tmp or dic[c] == tmp and  c < res):\n",
    "                tmp = dic[c]\n",
    "                res = c\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        dic={}\n",
    "        # 1、遍历数组，把偶数的出现次数统计到字典里。\n",
    "        for num in nums:\n",
    "            if num%2==0:\n",
    "                dic[num]=dic.get(num,0)+1\n",
    "        # dic没有值的情况\n",
    "        if not dic:\n",
    "            return -1\n",
    "        dd=sorted(dic.items(),key=lambda x:x[1],reverse=True)\n",
    "        # 2、找偶数出现次数最多的值，就是最大出现次数\n",
    "        ans=max([x for x in dic.values()])\n",
    "        # print(ans)\n",
    "        # 3、把出现最高频的偶数取出来，放到列表里\n",
    "        res=[key for key,value in dic.items() if value==ans]\n",
    "        # print(res)\n",
    "        # 如果列表不为空，取最小值。\n",
    "        if res:\n",
    "            return min(res)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        mp={}\n",
    "        for i in range(n):\n",
    "            if nums[i]%2==0:\n",
    "                mp[nums[i]]=nums.count(nums[i])\n",
    "        k1=[]\n",
    "        k2=[]\n",
    "        k3=[]\n",
    "        for key,value in mp.items():\n",
    "            k1.append(key)\n",
    "            k2.append(value)\n",
    "        if len(k1)==0:\n",
    "            return -1\n",
    "        ex1=max(k2)\n",
    "        ex2=len(k2)\n",
    "        for i in range (len(k2)):\n",
    "            j=i-len(k3)\n",
    "            if k2[j]==ex1:\n",
    "                k3.append(k1[k2.index(ex1)])\n",
    "                k1.remove(k1[k2.index(ex1)])\n",
    "                k2.remove(k2[k2.index(ex1)])\n",
    "        return min(k3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt = Counter(x for x in nums if x % 2 == 0)\n",
    "        if len(cnt) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            # print(cnt.most_common())\n",
    "            check_num= cnt.most_common()[0][0]\n",
    "            check_freq = cnt.most_common()[0][1]\n",
    "            final = check_num\n",
    "            for key, val in cnt.items():\n",
    "                if key != check_num and val == check_freq:\n",
    "                    final = min(final, key)\n",
    "            return final\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = 0\n",
    "        for k, v in Counter(nums).items():\n",
    "            if k % 2 == 0 and (v > cnt or (v == cnt and k < ans)):\n",
    "                ans = k\n",
    "                cnt =v\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        res = sorted([[k,v] for k,v in counter.items() if k % 2 == 0],key=lambda x:(-x[1],x[0]))\n",
    "        return res[0][0] if res else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ou = [i for i in nums if i%2==0]\n",
    "        if ou:\n",
    "            res_lst=[]\n",
    "            d = {}\n",
    "            for i in ou:\n",
    "        #         print(i)\n",
    "                if i not in d:\n",
    "                    d[i]=ou.count(i)\n",
    "        #     print(d)\n",
    "            freq = max(d.values())\n",
    "            for k,v in d.items():\n",
    "                if v==freq:\n",
    "                    res_lst.append(k)\n",
    "        #     print(min(res_lst))\n",
    "            return min(res_lst)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\r\n",
    "        frequency = 0\r\n",
    "        ans = list()\r\n",
    "        for i in list(set(nums)):\r\n",
    "            if i % 2 == 0:\r\n",
    "                if nums.count(i) > frequency:\r\n",
    "                    frequency = nums.count(i)\r\n",
    "                    ans.clear()\r\n",
    "                    ans.append(i)\r\n",
    "                elif nums.count(i) == frequency:\r\n",
    "                    ans.append(i)\r\n",
    "        if ans:\r\n",
    "            return min(ans)\r\n",
    "        else:\r\n",
    "            return -1\r\n",
    "                \r\n",
    "            \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        even_nums = [i for i in nums if i % 2 == 0]\n",
    "        if len(even_nums) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            count_unique = {i: even_nums.count(i) for i in set(nums)}\n",
    "            max_count = max(count_unique.values())\n",
    "            return min([k for k in count_unique if count_unique[k] == max_count])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        cnt = 0\n",
    "        for k, v in Counter(nums).items():\n",
    "            if k % 2 == 0 and (v > cnt or v == cnt and k < ans):\n",
    "                    ans = k\n",
    "                    cnt = v\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        dic={}\n",
    "        # 1、遍历数组，把偶数的出现次数统计到字典里。\n",
    "        for num in nums:\n",
    "            if num%2==0:\n",
    "                dic[num]=dic.get(num,0)+1\n",
    "        # dic没有值的情况\n",
    "        if not dic:\n",
    "            return -1\n",
    "        dd=sorted(dic.items(),key=lambda x:x[1],reverse=True)\n",
    "        # 2、找偶数出现次数最多的值，就是最大出现次数\n",
    "        ans=max([x for x in dic.values()])\n",
    "        # print(ans)\n",
    "        # 3、把出现最高频的偶数取出来，放到列表里\n",
    "        res=[key for key,value in dic.items() if value==ans]\n",
    "        # print(res)\n",
    "        # 如果列表不为空，取最小值。\n",
    "        if res:\n",
    "            return min(res)\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        # 找出所有偶数元素\n",
    "        dirct = {}\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i in dirct.keys():\n",
    "                   dirct[i] = dirct[i] + 1\n",
    "                else:\n",
    "                    dirct[i] = 1\n",
    "\n",
    "\n",
    "        print(dirct)\n",
    "\n",
    "        # 数偶数元素个数， 多个要最小的， 不存在 -1\n",
    "        maxv = 0\n",
    "        maxk = 0\n",
    "        for k,v in dirct.items():\n",
    "            if v >= maxv:\n",
    "                maxv = v\n",
    "                maxk = k\n",
    "\n",
    "        if maxv == 0:\n",
    "            return -1\n",
    "        \n",
    "        result = []\n",
    "        for k,v in dirct.items():\n",
    "            if v == maxv:\n",
    "                result.append(k)\n",
    "\n",
    "        print(result)\n",
    "        min = result[0]\n",
    "        for r in result:\n",
    "            if r <= min:\n",
    "                min = r\n",
    "        \n",
    "        return min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        myset = set(nums) #myset是另外一个列表，里面的内容是mylist里面的无重复 项\n",
    "        m=[]\n",
    "        m2=[]\n",
    "        for item in myset:\n",
    "          if item%2==0:\n",
    "             m.append(nums.count(item))\n",
    "\n",
    "        if m==[]:\n",
    "           return -1\n",
    "        \n",
    "        for item in myset:\n",
    "          if nums.count(item) == max(m) and item%2==0:\n",
    "             m2.append(item)\n",
    "\n",
    "        return min(m2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        return sorted([[1, -1]] + [[-v, k] for k, v in Counter(nums).items() if not (k % 2)])[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        tmp=dict(Counter(nums))\n",
    "        tmp=sorted(tmp.items(), key=lambda x: (-x[1],x[0]))\n",
    "        for i in tmp:\n",
    "            if i[0]%2==0:\n",
    "                return i[0]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        dic = {}\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i not in dic:\n",
    "                    dic[i] = nums.count(i)\n",
    "        dic = {k: v for k, v in sorted(dic.items(), key=lambda x: (-x[1], x[0]))}\n",
    "        if not dic:\n",
    "            ans = -1\n",
    "        else:\n",
    "            for k in dic.keys():\n",
    "                ans = k\n",
    "                break\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt=Counter(nums)\n",
    "        lst=[[k,v] for k,v in cnt.items() if k%2==0]\n",
    "        lst=sorted(lst,key=lambda v:(-v[1],v[0]))\n",
    "        return lst[0][0] if lst else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        ans_list = []\n",
    "        cnums = Counter(nums)\n",
    "        for k, v in cnums.most_common():\n",
    "            if k % 2 != 0:\n",
    "                continue\n",
    "            else:\n",
    "                if len(ans_list) == 0:\n",
    "                    ans_list.append([k, v])\n",
    "                elif ans_list[-1][1] == v:\n",
    "                    ans_list.append([k, v])\n",
    "        ans = sorted(ans_list, key=lambda x:x[0])\n",
    "        return ans[0][0] if len(ans) != 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums=sorted(collections.Counter(nums).items(), key=lambda x:(-x[1],x[0]))\n",
    "        for x,y in nums:\n",
    "            if x%2==0:\n",
    "                return x\n",
    "                break\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums.sort() #保证相同频次的时候小数在前\n",
    "        count = Counter(nums)  \n",
    "        c=count.most_common() #出现频次多的在前面，同频次小数在前\n",
    "        for i in c:\n",
    "            if i[0] % 2 == 0 : #上述保证了频次多的小数在前，这里保证为偶数，即返回出现次数最多的偶数中最小的那个\n",
    "                return i[0]\n",
    "        return -1 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums = [each for each in nums if each % 2 == 0]\n",
    "        if len(nums) == 0:\n",
    "            return -1\n",
    "        from collections import Counter\n",
    "        nums = Counter(nums)\n",
    "        nums = sorted(nums.items(), key=lambda x:(-x[1], x[0]))\n",
    "        return nums[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        f = Counter(nums)\n",
    "        if len(set(nums)) == 1:\n",
    "            return -1 if nums[0] % 2 else nums[0]\n",
    "        f1 = sorted(f.items(), key = lambda item: item[1], reverse = True)\n",
    "        try:\n",
    "            m = max([i[1] for i in f1 if not i[0] % 2])\n",
    "        except ValueError:\n",
    "            return -1\n",
    "        n = []\n",
    "        for i in f1:\n",
    "            if i[0] % 2 == 0:\n",
    "                if m > i[1]:\n",
    "                    break\n",
    "                n.append(i[0])\n",
    "        \n",
    "        return min([i[0] for i in f1 if m == i[1] and not i[0] % 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        d = Counter([i for i in nums if i%2 == 0])\n",
    "        return sorted(d.items(), key = lambda x: (-x[1], x[0]))[0][0] if d else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        res = sorted([[k,v] for k,v in counter.items() if k % 2 == 0],key=lambda x:(-x[1],x[0]))\n",
    "        return res[0][0] if res else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, li: List[int]) -> int:\n",
    "        max_num = max(li)\n",
    "        data = []\n",
    "        li_haxi = [0 for _ in range(max_num+1)]\n",
    "        for i in li:\n",
    "            li_haxi[i]+=1\n",
    "        for i in li_haxi[0::2]:\n",
    "            data.append(i)\n",
    "        ma_li = max(data)\n",
    "        if ma_li == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            p = 0\n",
    "            while True:\n",
    "                if data[p] == ma_li:\n",
    "                    return 2*p\n",
    "                else:\n",
    "                    p+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        cnt = [(k, v) for k, v in Counter(nums).items() if k % 2 == 0]\n",
    "        if not cnt:\n",
    "            return -1\n",
    "        return sorted(cnt, key = lambda x: (x[1], -x[0]))[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        arr = [0]*200000\n",
    "        m = len(nums)\n",
    "        for i in range(0,m):\n",
    "            if nums[i]%2 == 0:\n",
    "                arr[nums[i]]+=1\n",
    "        ret = 0\n",
    "        # print(arr)\n",
    "        for i in range(0,200000):\n",
    "            if arr[i] > arr[ret]:\n",
    "                ret = i\n",
    "        if sum(arr) == 0:\n",
    "            return -1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        hashtable = {}\n",
    "        ans = -1\n",
    "        count = 0\n",
    "        for i in nums:\n",
    "            if i%2 == 0:\n",
    "                if i in hashtable:\n",
    "                    hashtable[i] += 1\n",
    "                else:\n",
    "                    hashtable[i] = 1\n",
    "        print(hashtable)\n",
    "        if len(hashtable) > 0:\n",
    "            for k, v in hashtable.items():\n",
    "                if v > count:\n",
    "                    count = v\n",
    "                    ans = k\n",
    "                if v == count: \n",
    "                    if k < ans:\n",
    "                        ans = k\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        max_num = []\n",
    "        max_times = 0\n",
    "        ans = []\n",
    "        counter = Counter(nums)\n",
    "        #print(counter)\n",
    "        #print(dict(counter.items()))\n",
    "        for i in counter.items():\n",
    "            if i[0] % 2 == 0:\n",
    "                max_num.append(i)\n",
    "        for j in dict(max_num).items():\n",
    "            if j[1] == max(dict(max_num).values()):\n",
    "                ans.append(j[0])\n",
    "        ans.sort()\n",
    "        return ans[0] if len(ans) != 0 else -1\n",
    "        #print(counter.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        nums = Counter(nums)\n",
    "\n",
    "        result = 0\n",
    "        checklist = []\n",
    "\n",
    "        for num, cnt in nums.items():\n",
    "            if num % 2 == 0:\n",
    "                checklist.append([num,cnt])\n",
    "        if checklist == []:\n",
    "            return -1\n",
    "            \n",
    "        checklist.sort(key= lambda x:(-x[1], x[0]))\n",
    "        \n",
    "        return checklist[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        res = sorted([[k,v] for k,v in counter.items() if k % 2 == 0],key=lambda x:(-x[1],x[0]))\n",
    "        return res[0][0] if res else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def mostFrequentEven(self, nums: List[int]) -> int:\n",
    "        # 找出所有偶数元素\n",
    "        dirct = {}\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                if i in dirct.keys():\n",
    "                   dirct[i] = dirct[i] + 1\n",
    "                else:\n",
    "                    dirct[i] = 1\n",
    "\n",
    "\n",
    "        print(dirct)\n",
    "\n",
    "        # 数偶数元素个数， 多个要最小的， 不存在 -1\n",
    "        maxv = 0\n",
    "        maxk = 0\n",
    "        for k,v in dirct.items():\n",
    "            if v >= maxv:\n",
    "                maxv = v\n",
    "                maxk = k\n",
    "\n",
    "        if maxv == 0:\n",
    "            return -1\n",
    "        \n",
    "        result = []\n",
    "        for k,v in dirct.items():\n",
    "            if v == maxv:\n",
    "                result.append(k)\n",
    "\n",
    "        print(result)\n",
    "        min = result[0]\n",
    "        for r in result:\n",
    "            if r <= min:\n",
    "                min = r\n",
    "        \n",
    "        return min"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
